﻿@model IEnumerable<BenzInfo.Hockey.Grenadiers.Models.Partner>
@using BenzInfo.Hockey.Grenadiers.Models

@{
    ViewBag.Title = "Index"; 
}

<script type="text/javascript">

    $(document).ready(function () {

        $('.partners ul').disableSelection();

        $('.partners li').hover(function () {
            $('.buttons', this).show();
        }, function () {
            $('.buttons', this).hide();
        });

        $('#dialog_partners').dialog({
            autoOpen: false,
            modal: true,
            draggable: false,
            resizable: false,
            width: 580,
            height: 'auto',
            buttons: [
                {
                    id: 'dialog_btn_save', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Save',
                    click: function () {
                        $('form', this).submit();
                    }
                },
                {
                    id: 'dialog_btn_cancel', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Cancel',
                    click: function () {
                        $(this).dialog('close');
                    }
                }
            ]
        });

        $('.partners .buttons a').click(function () {
            var url = $(this).attr('href');
            var title = '';

            if (url.indexOf('Create') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Create';
                $('#dialog_btn_save span').html(title);
            }
            else if (url.indexOf('Edit') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Edit';
                $('#dialog_btn_save span').html('@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Save');
            }
            else if (url.indexOf('Delete') > 0) {
                title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Delete';
                $('#dialog_btn_save span').html(title);
            }

            $('#dialog_partners').empty().load(url, function () {
                $('#dialog_partners').dialog('option', 'title', title).dialog('open');
            });

            return false;
        });
    });

</script>
@if (ViewBag.CurrentUser != null)
{
    <script type="text/javascript">
        $(document).ready(function () {
            $('.partners ul').sortable({
                items: "li:not(:last-child)",
                stop: function (event, ui) {
                    var items = [];
                    $.each($('li:not(:last-child)', this), function (i, elem) {
                        items[i] = $(elem).attr('id').split('_')[1];
                    });

                    $.post('Partners/EditPosition', $.param({ items: items }, true));
                }
            });
        });
    </script>
}
<h1>@(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Partner)s</h1>

<div id="dialog_partners"></div>

@foreach (Partner.CategoryEnum category in Enum.GetValues(typeof(Partner.CategoryEnum)))
{
    var LabelCategory = @BenzInfo.Hockey.Grenadiers.Resources.Models.Partner.ResourceManager.GetString("CategoryEnum_" + category);

    <div class="partners">
        <h2>@LabelCategory</h2>
        <ul class="sortable">
        @foreach (var item in Model.Where(m => m.Category == category).OrderBy(m => m.Position))
        {
            if (ViewBag.CurrentUser != null || item.ShowInPartnersPage == true)
            {
                var picture_url = item.Picture ?? "img_no_picture_no.png";
                <li id="item_@(item.Id)">
                    @if (ViewBag.CurrentUser != null)
                    {
                        <span class="buttons">
                            @Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Edit, "Edit", new { id = item.Id })
                            @Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Delete, "Delete", new { id = item.Id })
                        </span>
                    }
                    <div class="image" style="background-image: url(@Url.Content("~/Utils/ImageHandler.ashx?src=partners/" + picture_url + "&maxwidth=225&maxheight=80"));">
                    @if (!string.IsNullOrEmpty(item.Url) && !string.IsNullOrEmpty(item.Picture))
                    {
                        <a href="@item.Url" target="_blank"></a>
                    }
                    else if (!string.IsNullOrEmpty(item.Url) && string.IsNullOrEmpty(item.Picture))
                    {
                        <a href="@item.Url" target="_blank">
                            <span class="thankyou">@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.ThankYou!</span>
                        </a>
                    }
                    else if (string.IsNullOrEmpty(item.Url) && string.IsNullOrEmpty(item.Picture))
                    {
                        <span class="thankyou">@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.ThankYou!</span>
                    }
                    </div>
                    <div class="info @(!item.ShowInPartnersPage ? "gray" : string.Empty)">
                        <b>@Html.DisplayFor(modelItem => item.Name)</b>
                        <span class="badge @category.ToString().ToLower()"></span>
                    </div>
                </li>
            }
        }
        @if (ViewBag.CurrentUser != null)
        {
            <li class="item_new" style="background-image: url(@Url.Content("~/Utils/ImageHandler.ashx?src=partners/img_no_picture_no.png"))">
                <span class="buttons">@Html.ActionLink(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Create, "Create", new { Category = (int)category })</span>
                <div class="image"><span class="thankyou">@LabelCategory</span></div>
                <div class="info new">
                    <b>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.AddPartner</b>
                    <span class="badge @category.ToString().ToLower()"></span>
                </div>
            </li>
        }
        </ul>
        <div class="clear"></div>
    </div>
}
